﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Web;
using System.Web.Security;
using System.Security.Principal;
using System.Threading;
//using TracerX;

using DocManagerORM.ORM;
using DocManagerORM.DB4O;
using System.IO;
using System.ComponentModel;
using System.Windows.Media.Animation;
using System.Diagnostics;

namespace DocManagerControls.Controls
{
    // if (DesignerProperties.GetIsInDesignMode(this)){

    public class BaseCusControl : UtilsBaseClass  //ContentControl //
    {        
        protected string m_Azione = "";
        Action<string> m_SwitchHandler;

        public static GLOBALcontrols DatiGlobaliControlli = new GLOBALcontrols();  

        public System.Windows.Controls.Menu MenuDescrittivo { get; set; }

        static BaseCusControl()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(BaseCusControl),
                new FrameworkPropertyMetadata(typeof(BaseCusControl)));

                //  prove_varie();           

        }//fm



        /// <summary>
        /// tipo "CSCUser2=Mahesh22
        /// la data viene aggiunta in automatico :expires=Sat, 10-Oct-2012 00:00:00 GMT"
        /// </summary>
        public void settaCookie(string pValoriDati, DateTime pScadenza,string pNomeCookie)
        {
            Uri uriCookies = new Uri(System.IO.Path.Combine(Environment.CurrentDirectory, pNomeCookie));
            string data = pValoriDati + ";expires=Sat, " + pScadenza.ToLongDateString() + " 00:00:00 GMT";
            Application.SetCookie(uriCookies, data);  
        
        }//fM

        public string getCookie(string pValoriDati, DateTime pScadenza, string pNomeCookie)
        {
            var uriCookies = new Uri(System.IO.Path.Combine(Environment.CurrentDirectory, pNomeCookie));
            return Application.GetCookie(uriCookies);  

        }//fM

        //private static  void prove_varie()
        //{
        //    //var sql = new DocManagerORM.Utils.SqlServer("DocManagerDb", true);
        //    //sql.PopolaClassiPOCOdaDB(null);
        //    //sql.GeneraClassiPocodaDb("ODXOrmLayer.Entità", true);

        //    var dbOPF = new DocManagerORM.OPF3.OPF3Database(DocManagerORM.OPF3.DBEnum.MsSql,
        //         getValueFromAppConfig("DocManagerDb"));

        //   var pathDir = System.Configuration.ConfigurationManager.AppSettings["DB4ODirectoryPath"];
        //   if (string.IsNullOrEmpty(pathDir)) pathDir = Environment.CurrentDirectory;
        //   if (!Directory.Exists(pathDir)) pathDir = Environment.CurrentDirectory;

        //    DB4OBase db4 = new DB4OBase("DocManDB.db4o", pathDir );

        //    try
        //    {
        //        var docs = dbOPF.getObjectSet<DocManagerORM.Entità.Documenti>("");

        //        //db4.UpdateObjects<DocManagerORM.Entità.Documenti>(docs);
        //        var docs2 = db4.queryOggetti<DocManagerORM.Entità.Documenti>(null);
             
        //    }
        //    catch (Exception ex)
        //    {
        //        var a = ex.Message;
        //    }


        //    try
        //    {
        //        var qu = new DocManagerORM.ORM.QueryCodifiche();
        //        var catList = qu.getCATEGORIE(null).ToList();

        //        db4.UpdateObjects(catList);

        //        var listRet = db4.queryOggetti<Categorie>(null);

        //        db4.DeframmentaDatabase();
        //        db4.BackupDB("MyBackup.db4.bak");
        //    }
        //    catch (Exception ex)
        //    {
        //        var a = ex.Message;               
        //    }
            
        //}

        //TracerX.TraceLevel
        public static void Logga(string pMessaggio,TraceEventType pLevel= TraceEventType.Information)
        {
            DS2171Utils.TheLogger.loggaNet(pMessaggio, pLevel); 
              
        }//fM

        
        public static string getValueFromAppConfig(string pName, bool pIsFromAppSettings = false)
        {
            if (pIsFromAppSettings)
                return System.Configuration.ConfigurationManager.AppSettings[pName].FirstOrDefault().ToString();
            else
                return System.Configuration.ConfigurationManager.ConnectionStrings[pName].ConnectionString;

            return string.Empty;

        }//fM
        

        public bool UtenteIsInRole(string pRuolo)
        {
            return (Thread.CurrentPrincipal.IsInRole(pRuolo) ? true : false);
        }//fM

        public Frame getFrameContainer()
        {
            dynamic winMain = Application.Current.MainWindow;
            return winMain.FrameContainer;
        }//fM

        public void addMenuHandlerToMenu(Action<string> pSwitchHandler )
        {
            m_SwitchHandler = pSwitchHandler;         
        }//fM

        public void addMenuItemsToMenu(params MenuItem[] pMenuItems)
        {

            if (MenuDescrittivo == null) tryGetMenu();
            if (MenuDescrittivo == null) return;

            foreach (var item in pMenuItems)
            {
                MenuDescrittivo.Items.Add(item);   
            }
           
        }//fM



        public Storyboard tryGetEffetti()
        {
            var story = Template.FindName("RotazioneControllo", this) as System.Windows.Media.Animation.Storyboard;

            if (story == null) return null;

            return story;           
        
        }//fM

        private void tryGetMenu()
        {
            var menu = Template.FindName("menuPage", this) as Menu;

            if (menu == null)
            {
                messaggia("Menu della classe base NON trovato"); return;
            }

            MenuDescrittivo = menu;
            menu.AddHandler(MenuItem.ClickEvent, new RoutedEventHandler(menuPage_Click)); 
        
        }//fM

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            tryGetMenu();                   
        }   
        
        private void menuPage_Click(object sender, RoutedEventArgs e)
        {
            MenuHandler(e);
        }//fm

        private void MenuHandler(RoutedEventArgs e)
        {
            m_Azione = ((MenuItem)e.OriginalSource).Header.ToString().ToLower();
            
            switch (m_Azione)
            {
                case "exit":
                    var par = this.Parent;
                    this.Visibility = System.Windows.Visibility.Hidden;                    
                    break;               
            }

            if (m_SwitchHandler == null) return;

            m_SwitchHandler(m_Azione); //menu del controllo derivato

        }//fM

       

        protected void messaggia(string pMessage)
        {
            MessageBox.Show(pMessage, "Avvisi", MessageBoxButton.OK, MessageBoxImage.Information);
        }//fM

       

    }//fC
}
